﻿using AbpApplication.InPut;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using NewWms;
using NewWms.Apiservice;
using NewWms.Apiservice.Dto;
using NewWms.Extend;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using Reconstruct_Desktop.Apiservice.Dto;
using Reconstruct_Desktop.Apiservice.InPut;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WcsZz.Extend;

namespace Reconstruct_Desktop.ViewModels.DialogsModel
{
    public partial class StockTakingDialogModel : BindableBase, IDialogAware
    {

        private readonly IBaseService_Client _baseService_Client;
        private readonly IViewService_Client _viewService_Client;

        private InPut_ViewService15_Client inPut_ViewService15_Client;
        public InPut_ViewService15_Client InPut_ViewService15_Client
        {
            get { return inPut_ViewService15_Client; }
            set { inPut_ViewService15_Client = value;RaisePropertyChanged();}
        }

        private List<CustomCheckListDto> customCheckListDtos;
        public List<CustomCheckListDto> CustomCheckListDtos
        {
            get { return customCheckListDtos; }
            set { customCheckListDtos = value;RaisePropertyChanged(); }
        }
        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }

        public StockTakingDialogModel()
        {
           this. _baseService_Client = new BaseService_Client();
           this. _viewService_Client = new ViewService_Client();
           InPut_ViewService15_Client=new InPut_ViewService15_Client();
           CustomCheckListDtos= new List<CustomCheckListDto>();
            InPut_ViewService15_Client.PageIndex = 1;
            InPut_ViewService15_Client.PageSize = 50;
            GetView();
        }


        //视图获取
        [RelayCommand]
        public async void GetView()
        {
         
            
            Tuple<List<CustomCheckListDto>, long?, long?> tuple = await _viewService_Client.View_CoutomerCheckList(InPut_ViewService15_Client);
            if (tuple.Item1 != null)
            {
                CustomCheckListDtos = tuple.Item1;
                Count = (long)tuple.Item2;
                PageCount = (long)tuple.Item3;
            }

        }


        //盘点单号
        [RelayCommand]
        public async void Start_StockTaking(System.Collections.IList SelectItems)
        {
            InPut_BaseService26_Client inPut_BaseService26 = new InPut_BaseService26_Client();
            foreach (CustomCheckListDto item in SelectItems)
            {

                inPut_BaseService26.CustomCheckList_Id.Add(item.CustomCheckList_Id);

            }
            inPut_BaseService26.Machine = int.Parse( GlobalVariable.Machine);
            bool res=  await _baseService_Client.Start_StockTaking(inPut_BaseService26);
            if (res)
            {

                GetView();
                EventAggregatorHelper.EventAggregator.SendMessage("修改成功", "跳转页面");
                // 调用 RequestClose 关闭 Dialog
                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
            }
        }
        // 删除
        [RelayCommand]
        public async void DeleteCustomCheckList(System.Collections.IList SelectItems)
        {
            await  _baseService_Client.DeleteCustomCheckList(SelectItems.Cast<CustomCheckListDto>().Select(x => x.CustomCheckList_Id).ToList());
            GetView();
        }

        //导入
        [RelayCommand]
        public async void Import()
        {
            ExcelImport.ExcelImportStockTaking();
        }


        //basic setting
        public string Title { get; set; } = "物料盘点";
        public event Action<IDialogResult> RequestClose;
        public bool CanCloseDialog()
        {
            return true;
        }
        public void OnDialogClosed()
        {
            
        }
        public void OnDialogOpened(IDialogParameters parameters)
        {
           
        }
    }
}
